package cn.com.gas.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasDeclaretaxEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.entity.GasStationinfoForecastMonthEntity;
import cn.com.gas.service.GasDeclaretaxService;
import cn.com.gas.service.GasStationinfoForecastMonthService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.gas.vo.excel.GasDeclaretaxExcel;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

/**
 * 申报比对
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-12 16:28:51
 */
@Controller
@RequestMapping("/gas/declaretax")
public class GasDeclaretaxController {
	@Autowired
	private GasDeclaretaxService gasDeclaretaxService;
	
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	/*@Autowired
	private GasCaminfoForecastService gasCaminfoForecastService;*/
	/*@Autowired
	private GasStationinfoForecastService gasStationinfoForecastService;*/
	
	@Autowired
	private GasStationinfoForecastMonthService gasStationinfoForecastMonthService;
	
	
	
	@Value("${file.path}")
	private String filePath;
	@Value("${file.url}")
	private String fileUrl;

	@ApiOperation(value = "新增")
	@PostMapping("/add")
	// @RequiresPermissions("gasDeclaretax:add")
	@ResponseBody
	public DataResult add(@RequestBody GasDeclaretaxEntity gasDeclaretax) {

		if (TsBqUtils.isNull(gasDeclaretax.getSale()) || TsBqUtils.isNull(gasDeclaretax.getDeclaretaxMoney())) {
			return DataResult.fail("sale或declaretaxMoney入参为null!");
		}

		gasDeclaretax.setComparison(computeComparison(gasDeclaretax.getSale(), gasDeclaretax.getDeclaretaxMoney()));

		gasDeclaretax.setDeleted(Constants.DELETEED_NOT);
		gasDeclaretax.setCreateId(httpSessionService.getCurrentUserId());
		gasDeclaretax.setCreateTime(new Date());

		gasDeclaretaxService.save(gasDeclaretax);
		return DataResult.success();
	}

	
	@ApiOperation(value = "更新")
	@PutMapping("/update")
	// @RequiresPermissions("gasDeclaretax:update")
	@ResponseBody
	public DataResult update(@RequestBody GasDeclaretaxEntity gasDeclaretax) {
		if (TsBqUtils.isNull(gasDeclaretax.getSale()) || TsBqUtils.isNull(gasDeclaretax.getDeclaretaxMoney())) {
			return DataResult.fail("sale或declaretaxMoney入参为null!");
		}

		gasDeclaretax.setComparison(computeComparison(gasDeclaretax.getSale(), gasDeclaretax.getDeclaretaxMoney()));

		gasDeclaretax.setUpdateId(httpSessionService.getCurrentUserId());
		gasDeclaretax.setUpdateTime(new Date());

		gasDeclaretaxService.updateById(gasDeclaretax);
		return DataResult.success();
	}
	// TODO
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	// @RequiresPermissions("gasDeclaretax:list")
	@ResponseBody
	public DataResult findListByPage(@RequestBody GasDeclaretaxEntity gasDeclaretax) throws ParseException {
		
		String createTimeStart = gasDeclaretax.getCreateTimeStart();
		
		String taxorgcode = gasDeclaretax.getTaxorgcode();

		String currentDeptId = httpSessionService.getCurrentDeptId();

		Page page = new Page(gasDeclaretax.getPage(), gasDeclaretax.getLimit());
		LambdaQueryWrapper<GasStationinfoEntity> queryWrapper = Wrappers.lambdaQuery();

		if (TsBqUtils.isNotNull(taxorgcode)) {
			queryWrapper.eq(GasStationinfoEntity::getType, "group");
			queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, taxorgcode);

		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, currentDeptId);

			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				queryWrapper.like(GasStationinfoEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");

			}
		}

		if (TsBqUtils.isNotNull(gasDeclaretax.getStationName())) {
			queryWrapper.like(GasStationinfoEntity::getName, gasDeclaretax.getStationName());
		}
		
		IPage<GasStationinfoEntity> iPage = gasStationinfoService.page(page, queryWrapper);

		List<GasStationinfoEntity> records = iPage.getRecords();
		
		for (GasStationinfoEntity gasStationinfoEntity : records) {
			// 处理加油站 名称
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}
			// 税务机关
			if (TsBqUtils.isNotNull(gasStationinfoEntity.getTaxorgcode())) {
				SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
				gasStationinfoEntity.setTaxorgName(sysDept.getName());
			}

			// 计算累计金额
			BigDecimal gatherValueDecimal = new BigDecimal(0);
			BigDecimal forecastValueDecimal = new BigDecimal(0);
			BigDecimal offlineDecimal = new BigDecimal(0);

			String id = gasStationinfoEntity.getId();

			String gasTaxorgcode = gasStationinfoEntity.getTaxorgcode();
			
			gasStationinfoEntity.setGatherValue(gatherValueDecimal.toString());
			gasStationinfoEntity.setForecastValue(forecastValueDecimal.toString());

			gasStationinfoEntity.setOfflineTime(offlineDecimal.toString());
			
			String yearMonths = createTimeStart;
			
			if (TsBqUtils.isNotNull(yearMonths) && TsBqUtils.isNotNull(gasTaxorgcode) && TsBqUtils.isNotNull(id)) {
				
				String laterMonthYear = laterMonthYear(Integer.parseInt(yearMonths.substring(4, 6)),
						Integer.parseInt(yearMonths.substring(0, 4)));
				
				LambdaQueryWrapper<GasDeclaretaxEntity> queryWrapperDeclaretax = Wrappers.lambdaQuery();
				// 查询条件示例
				queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getDeclaretaxTime, laterMonthYear);
				queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getTaxorgcode, gasTaxorgcode);
				queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getStationId, id);

				List<GasDeclaretaxEntity> listGasDeclaretax = gasDeclaretaxService.list(queryWrapperDeclaretax);
				if (TsBqUtils.isNotNull(listGasDeclaretax)) {
					GasDeclaretaxEntity declaretax = listGasDeclaretax.get(0);
					String declaretaxMoney = declaretax.getDeclaretaxMoney();
					String comparison = declaretax.getComparison();
					String declaretaxId = declaretax.getId();
					gasStationinfoEntity.setDeclaretaxMoney(declaretaxMoney);
					gasStationinfoEntity.setComparison(comparison);
					gasStationinfoEntity.setDeclaretaxId(declaretaxId);
				}
				
				gasStationinfoEntity.setDeclaretaxTime(laterMonthYear);
				gasStationinfoEntity.setYearMonths(createTimeStart);
				
				// 计算  forecastValue1 gasLocation1 gasCoverage1
				LambdaQueryWrapper<GasStationinfoForecastMonthEntity> queryWrapper2 = Wrappers.lambdaQuery();
				queryWrapper2.eq(GasStationinfoForecastMonthEntity::getStationinfoId, id);
				
				queryWrapper2.eq(GasStationinfoForecastMonthEntity::getTime, createTimeStart.substring(0, 4) + "-" + createTimeStart.substring(4, 6));

				List<GasStationinfoForecastMonthEntity> list1 = gasStationinfoForecastMonthService.list(queryWrapper2);
				
				
				if(TsBqUtils.isNotNull(list1)){
					
					BigDecimal forecastValueDecimal1 = new BigDecimal(0);
					BigDecimal gatherValueDecimal1 = new BigDecimal(0);
					for (GasStationinfoForecastMonthEntity _gasStationinfoForecast : list1) {
						
						String forecast = "0";
						String gather = "0";
						String forecastValue = _gasStationinfoForecast.getForecastValue();
						String gatherValue = _gasStationinfoForecast.getGatherValue();
						
						if(TsBqUtils.isNotNull(forecastValue) && TsBqUtils.isNotNull(gatherValue)){
							forecast = forecastValue;
							gather = gatherValue;
						}
						
						BigDecimal _forecastValue = new BigDecimal(forecast);
						
						BigDecimal _gatherValue = new BigDecimal(gather);
						
						forecastValueDecimal1 = forecastValueDecimal1.add(_forecastValue);
						gatherValueDecimal1 = gatherValueDecimal1.add(_gatherValue);
								
					}
					
					gasStationinfoEntity.setForecastValue1(forecastValueDecimal1.toString());
					gasStationinfoEntity.setGatherValue(gatherValueDecimal1.toString());
					
				}
				
				
			}
			
		}

		return DataResult.success(iPage);

	}

	@PostMapping("/exportData")
	@ResponseBody
	public DataResult exportData(@RequestBody GasDeclaretaxEntity gasDeclaretax)
			throws java.io.IOException, ParseException {
		
		String createTimeStart = gasDeclaretax.getCreateTimeStart();
		
		String _taxorgcode = gasDeclaretax.getTaxorgcode();

		String currentDeptId = httpSessionService.getCurrentDeptId();

		// Page page = new Page(gasDeclaretax.getPage(), gasDeclaretax.getLimit());
		LambdaQueryWrapper<GasStationinfoEntity> queryWrapper = Wrappers.lambdaQuery();

		if (TsBqUtils.isNotNull(_taxorgcode)) {
			queryWrapper.eq(GasStationinfoEntity::getType, "group");
			queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, _taxorgcode);

		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, currentDeptId);

			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				queryWrapper.like(GasStationinfoEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");

			}
		}

		if (TsBqUtils.isNotNull(gasDeclaretax.getStationName())) {
			queryWrapper.like(GasStationinfoEntity::getName, gasDeclaretax.getStationName());
		}
		
		List<GasStationinfoEntity> _list = gasStationinfoService.list(queryWrapper);

		for (GasStationinfoEntity gasStationinfoEntity : _list) {
			

			// 处理加油站 名称
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}
			// 税务机关
			if (TsBqUtils.isNotNull(gasStationinfoEntity.getTaxorgcode())) {
				SysDept sysDept = deptService.getById(gasStationinfoEntity.getTaxorgcode());
				gasStationinfoEntity.setTaxorgName(sysDept.getName());
			}

			// 计算累计金额
			BigDecimal gatherValueDecimal = new BigDecimal(0);
			BigDecimal forecastValueDecimal = new BigDecimal(0);
			BigDecimal offlineDecimal = new BigDecimal(0);

			String id = gasStationinfoEntity.getId();

			String gasTaxorgcode = gasStationinfoEntity.getTaxorgcode();
			
			gasStationinfoEntity.setGatherValue(gatherValueDecimal.toString());
			gasStationinfoEntity.setForecastValue(forecastValueDecimal.toString());

			gasStationinfoEntity.setOfflineTime(offlineDecimal.toString());
			
			String yearMonths = createTimeStart;
			
			if (TsBqUtils.isNotNull(yearMonths) && TsBqUtils.isNotNull(gasTaxorgcode) && TsBqUtils.isNotNull(id)) {
				
				String laterMonthYear = laterMonthYear(Integer.parseInt(yearMonths.substring(4, 6)),
						Integer.parseInt(yearMonths.substring(0, 4)));
				
				LambdaQueryWrapper<GasDeclaretaxEntity> queryWrapperDeclaretax = Wrappers.lambdaQuery();
				// 查询条件示例
				queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getDeclaretaxTime, laterMonthYear);
				queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getTaxorgcode, gasTaxorgcode);
				queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getStationId, id);

				List<GasDeclaretaxEntity> listGasDeclaretax = gasDeclaretaxService.list(queryWrapperDeclaretax);
				if (TsBqUtils.isNotNull(listGasDeclaretax)) {
					GasDeclaretaxEntity declaretax = listGasDeclaretax.get(0);
					String declaretaxMoney = declaretax.getDeclaretaxMoney();
					String comparison = declaretax.getComparison();
					String declaretaxId = declaretax.getId();
					gasStationinfoEntity.setDeclaretaxMoney(declaretaxMoney);
					gasStationinfoEntity.setComparison(comparison);
					gasStationinfoEntity.setDeclaretaxId(declaretaxId);
				}
				
				gasStationinfoEntity.setDeclaretaxTime(laterMonthYear);
				gasStationinfoEntity.setYearMonths(createTimeStart);
				
				// 计算  forecastValue1 gasLocation1 gasCoverage1
				LambdaQueryWrapper<GasStationinfoForecastMonthEntity> queryWrapper2 = Wrappers.lambdaQuery();
				queryWrapper2.eq(GasStationinfoForecastMonthEntity::getStationinfoId, id);
				
				queryWrapper2.eq(GasStationinfoForecastMonthEntity::getTime, createTimeStart.substring(0, 4) + "-" + createTimeStart.substring(4, 6));

				List<GasStationinfoForecastMonthEntity> list1 = gasStationinfoForecastMonthService.list(queryWrapper2);
				
				
				if(TsBqUtils.isNotNull(list1)){
					
					BigDecimal forecastValueDecimal1 = new BigDecimal(0);
					BigDecimal gatherValueDecimal1 = new BigDecimal(0);
					for (GasStationinfoForecastMonthEntity _gasStationinfoForecast : list1) {
						
						String forecast = "0";
						String gather = "0";
						String forecastValue = _gasStationinfoForecast.getForecastValue();
						String gatherValue = _gasStationinfoForecast.getGatherValue();
						
						if(TsBqUtils.isNotNull(forecastValue) && TsBqUtils.isNotNull(gatherValue)){
							forecast = forecastValue;
							gather = gatherValue;
						}
						
						BigDecimal _forecastValue = new BigDecimal(forecast);
						
						BigDecimal _gatherValue = new BigDecimal(gather);
						
						forecastValueDecimal1 = forecastValueDecimal1.add(_forecastValue);
						gatherValueDecimal1 = gatherValueDecimal1.add(_gatherValue);
								
					}
					
					gasStationinfoEntity.setForecastValue1(forecastValueDecimal1.toString());
					gasStationinfoEntity.setGatherValue(gatherValueDecimal1.toString());
					
				}
				
				
			}
			
		}
		
		if (_list.size() > 0) {

			List<GasDeclaretaxExcel> gasDeclaretaxExcelList = new ArrayList<>();

			for (GasStationinfoEntity entity : _list) {

				String yearMonths = entity.getYearMonths();
				String taxorgcode = entity.getTaxorgcode();
				String stationId = entity.getId();

				if (TsBqUtils.isNotNull(yearMonths) && TsBqUtils.isNotNull(taxorgcode)
						&& TsBqUtils.isNotNull(stationId)) {
					String laterMonthYear = laterMonthYear(Integer.parseInt(yearMonths.substring(4, 6)),
							Integer.parseInt(yearMonths.substring(0, 4)));
					LambdaQueryWrapper<GasDeclaretaxEntity> queryWrapperDeclaretax = Wrappers.lambdaQuery();
					// 查询条件示例
					queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getDeclaretaxTime, laterMonthYear);
					queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getTaxorgcode, taxorgcode);
					queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getStationId, stationId);

					List<GasDeclaretaxEntity> listGasDeclaretax = gasDeclaretaxService.list(queryWrapperDeclaretax);
					if (TsBqUtils.isNotNull(listGasDeclaretax)) {
						GasDeclaretaxEntity declaretax = listGasDeclaretax.get(0);
						entity.setDeclaretaxMoney(declaretax.getDeclaretaxMoney());
						entity.setComparison(declaretax.getComparison());
						entity.setDeclaretaxId(declaretax.getId());
					}
					entity.setDeclaretaxTime(laterMonthYear);
				}

				if (TsBqUtils.isNotNull(taxorgcode)) {
					SysDept sysDept = deptService.getById(taxorgcode);
					String name = sysDept.getName();
					entity.setTaxorgName(name);
				}

				GasDeclaretaxExcel declaretaxExcel = GasDeclaretaxExcel.builder()
						.stationName(entity.getName())
						.taxorgName(entity.getTaxorgName())
						.sale(entity.getGatherValue())
						.forecastValue(entity.getForecastValue1())
						.declaretaxMoney(entity.getDeclaretaxMoney())
						.comparison(entity.getComparison())
						.yearMonths(entity.getYearMonths())
						.taxorgcode(entity.getTaxorgcode())
						.stationId(entity.getId()).build();

				gasDeclaretaxExcelList.add(declaretaxExcel);

			}

			String fileName = TsBqUtils.getUUID() + ".xlsx";

			String excelPath = filePath + fileName;
			OutputStream out = null;

			try {
				out = new FileOutputStream(excelPath);
				ExcelWriter writer = EasyExcelFactory.getWriter(out);

				Sheet sheet1 = new Sheet(1, 0, GasDeclaretaxExcel.class);
				sheet1.setSheetName("数据");
				writer.write(gasDeclaretaxExcelList, sheet1);
				// 将上下文中的最终 outputStream 写入到指定文件中
				writer.finish();
				File excelFile = new File(excelPath);
				List<Object> excelFileInfo = new ArrayList<>();
				excelFileInfo.add(fileName);
				excelFileInfo.add(excelFile.length());
				excelFileInfo.add(fileUrl + "/" + fileName);

				return DataResult.success(excelFileInfo);

			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return DataResult.fail("xlsx生成失败：" + e.toString());
			} finally {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
					return DataResult.fail("xlsx生成失败：" + e.toString());
				}

			}

		} else {
			return DataResult.fail("传入的ids为null！");
		}
	}

	@PostMapping("/importData")
	@ResponseBody
	public DataResult importData(@RequestParam("file") MultipartFile[] files) {

		if (files != null && files.length > 0) {
			for (MultipartFile file : files) {
				InputStream inputStream = null;
				try {
					inputStream = file.getInputStream();
					Sheet sheet = new Sheet(1, 1);
					List<Object> readList = EasyExcelFactory.read(inputStream, sheet);
					List<GasDeclaretaxEntity> declaretaxList = new ArrayList<>();

					for (Object object : readList) {
						List<String> list = TsBqUtils.castList(object, String.class);
						String stationName = list.get(0);//
						String taxorgName = list.get(1); //
						String sale = list.get(2); //
						// String gatherValue = list.get(3); //
						
						String declaretaxMoney = list.get(4); //
						// String comparison = list.get(5); //
						String yearMonths = list.get(6); //
						String taxorgcode = list.get(7); //
						String stationId = list.get(8); //

						if (TsBqUtils.isNull(stationName)) {
							return DataResult.fail("表格中存在【加油站名称】为空的！");
						}
						
						if (TsBqUtils.isNull(taxorgName)) {
							return DataResult.fail("表格中存在【税务机关】为空的！");
						}
						
						if (TsBqUtils.isNull(declaretaxMoney)) {
							return DataResult.fail("表格中存在【申报金额】为空的！");
						}
						
						if (TsBqUtils.isNull(yearMonths)) {
							return DataResult.fail("表格中存在【销售月份】为空的！");
						}
						if (TsBqUtils.isNull(taxorgcode)) {
							return DataResult.fail("表格中存在【税务机关代码】为空的！");
						}
						if (TsBqUtils.isNull(stationId)) {
							return DataResult.fail("表格中存在【加油站编号】为空的！");
						}

						GasDeclaretaxEntity declaretax;
						String laterMonthYear = laterMonthYear(Integer.parseInt(yearMonths.substring(4, 6)),
								Integer.parseInt(yearMonths.substring(0, 4)));
						LambdaQueryWrapper<GasDeclaretaxEntity> queryWrapperDeclaretax = Wrappers.lambdaQuery();
						// 查询条件示例
						queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getDeclaretaxTime, laterMonthYear);
						queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getTaxorgcode, taxorgcode);
						queryWrapperDeclaretax.eq(GasDeclaretaxEntity::getStationId, stationId);

						List<GasDeclaretaxEntity> listGasDeclaretax = gasDeclaretaxService.list(queryWrapperDeclaretax);

						if (TsBqUtils.isNotNull(listGasDeclaretax)) {
							// 修改逻辑
							declaretax = listGasDeclaretax.get(0);

							declaretax.setUpdateId(httpSessionService.getCurrentUserId());
							declaretax.setUpdateTime(new Date());
						} else {
							// 新增逻辑
							declaretax = new GasDeclaretaxEntity();

							declaretax.setStationName(stationName);
							declaretax.setTaxorgcode(taxorgcode);
							declaretax.setDeclaretaxTime(laterMonthYear);

							declaretax.setTaxorgcode(taxorgcode);
							declaretax.setStationId(stationId);

							declaretax.setDeleted(Constants.DELETEED_NOT);
							declaretax.setCreateId(httpSessionService.getCurrentUserId());
							declaretax.setCreateTime(new Date());

						}

						declaretax.setDeclaretaxMoney(declaretaxMoney);
						declaretax.setComparison(computeComparison(sale, declaretaxMoney));

						declaretaxList.add(declaretax);
					}

					if (TsBqUtils.isNotNull(declaretaxList)) {
						for (GasDeclaretaxEntity entity : declaretaxList) {
							gasDeclaretaxService.saveOrUpdate(entity);

						}
					}

				} catch (Exception e) {
					System.out.println(e.getMessage());
				} finally {
					if (inputStream != null) {
						try {
							inputStream.close();
						} catch (IOException e) {
							e.printStackTrace();
							return DataResult.fail(e.getMessage());
						}
					}
				}
			}
		}

		return DataResult.success();
	}

	private static String handleMonthYear(int month, int year) {
		if (month <= 9) {
			return year + "0" + month;
		} else {
			return year + "" + month;
		}
	}

	private static String laterMonthYear(int month, int year) {
		if (month == 12) {
			return year + 1 + "01";
		} else {
			int _month = month + 1;
			return handleMonthYear(_month, year);
		}
	}

	private static String computeComparison(String _sale, String _declaretaxMoney) {
		if (TsBqUtils.isNotNull(_sale) && TsBqUtils.isNotNull(_declaretaxMoney)) {
			if (!_sale.equals("0") && !_declaretaxMoney.equals("0")) {
				BigDecimal sale = new BigDecimal(_sale);
				BigDecimal declaretaxMoney = new BigDecimal(_declaretaxMoney);

				BigDecimal result = sale.divide(declaretaxMoney, 2, BigDecimal.ROUND_HALF_UP);

				return result.multiply(new BigDecimal(100)).toString() + "%";
			}
		}
		return "0%";
	}
	
	public static void main(String[] args) {
		System.out.println(laterMonthYear(12,2024));
	}
	
}
